home *** CD-ROM | disk | FTP | other *** search
- Path: news.cs.hope.edu!vnopstal
- From: vnopstal@cs.hope.edu (Michael Van Opstall)
- Newsgroups: comp.lang.c++
- Subject: Re: How to make a Complex class
- Date: 2 Jan 1996 20:12:01 GMT
- Organization: Hope College
- Message-ID: <4cc3ih$9if@news.cs.hope.edu>
- References: <4c8kdm$p0q@news.csie.nctu.edu.tw> <4c984a$99t@atlas.axiom.net> <4c9vvd$rjh@news.csie.nctu.edu.tw>
- NNTP-Posting-Host: smaug.cs.hope.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- NortonNg (jkng@csie.nctu.edu.tw) wrote:
- : Dave Nulton (dnult@axiom.net) ┤ú¿∞:
- : : I've just been working with complex classes myself in C++. The
- : : class is in there. There is already a library installed in my
- : : compiler - use it by including <complex.h>. It defines a
- : : structure you use called complex. Define a number something like
- : : this.
- : : complex(double r, double i);
- : : As far as your format (typical mathmatical format) you'll have to
- : : format you output and parse you input to make it work properly.
- : : The advantage to the complex library is that most all of the math
- : : functions and stream operators have been overloaded to handle the
- : : complex math. In addition there are several standard functions.
- : : The only thing not included is polar operators, but those are easy
- : : to construct.
- : : Hope that helps
- : : -dnult
-
- : Thanks for you help, but I still have a problem can not be overcomed.
- : That is how to write to function which parse my input to make the complex
- : class work properly. For instance, if I input (3+4.5i)*(2-8i)-(5i)
- : the function must parse it and then do the correct answer.
- : I don't know how to make it, hope you can make it. Thanks....
-
-
- If you know how many complex number operations are going to take place,
- you can simply parse the input using cin like this:
-
- char paren, op1, op2, op3, ichar;
- double r1, r2, i1, i2;
-
- cin >>paren>>r1>>op1>>i1>>ichar>>paren>>op2>>paren>>r2>>op3>>i2>>ichar>>paren;
-
- if (op1=='-') i1=-i1;
- if (op3=='+') i2=-i2;
-
- Complex c1(r1,i1);
- Complex c2(r2,i2);
- Complex c3;
-
- switch(op2) {
- case '+':
- c3=c1+c2;
- break;
- case '-':
- c3=c1-c2;
- break;
- case '*':
- c3=c1*c2;
- break;
- case '/':
- c3=c1/c2;
- break;
- }
-
- assuming you have overloaded the ops for complex nums. Otherwise, parsing of
- mathematical input can be achieved using what is known as a finite state
- automaton (I know little about this, about 1 day in my c++ class) which would
- also effectively strip away white space.
-
- --
- Michael A. Van Opstall -- vnopstal@cs.hope.edu
- http://www.cs.hope.edu/~vnopstal/deal.html - try it
-